package com.example.demo.service;

import com.example.demo.dao.WxUserInfoDAO;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizArt;
import com.example.demo.po.WxUserInfo;
import com.example.demo.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxUserInfoService {

    @Autowired
    WxUserInfoDAO wxUserInfoDAO;

    public PageResult findByPage(WxUserInfo user, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<WxUserInfo> list = wxUserInfoDAO.list(user);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<WxUserInfo>(list));
        return pageResult;
    }


    public boolean saveOrUpdate(WxUserInfo wxUserInfo){
        int i = wxUserInfoDAO.saveorUpdate(wxUserInfo);
        if(i>0)
            return true;
        else
            return false;
    }

    /**
     * 判断用户是否注册过
     * @param openid
     * @return
     */
    public boolean wUserIsLog(String openid){
        if(openid==null||"".equals(openid.trim()))
            return false;
        WxUserInfo user = wxUserInfoDAO.findById(openid);
        if(user!=null && (user.getNickName()!=null && !"".equals(user.getNickName())))
            return true;
        else
            return false;
    }

    public WxUserInfo getById(String id){
        return  wxUserInfoDAO.findById(id);
    }


}
